package com.example.consumer.config;

import feign.RetryableException;
import feign.Retryer;

import java.time.LocalDateTime;

public class CustomRetryer implements Retryer {
    private final int maxAttempts; //最大尝试次数
    private final long period;   // 重试间隔时间
    private int attempt;      // 当前尝试次数

    public CustomRetryer() {
        maxAttempts = 3;
        attempt = 1;
        period = 1000;
    }
    @Override
    public void continueOrPropagate(RetryableException e) {
        if (this.attempt++ >= this.maxAttempts) {
            throw e;
        }
        long interval = this.period;
        System.out.println(LocalDateTime.now() + "| 执行一次重试：" + interval);
        try {
            Thread.sleep(interval*(attempt));
        } catch (InterruptedException ex) {
            throw new RuntimeException(ex);
        }
    }

    @Override
    public Retryer clone() {
        return null;
    }
}
